package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.OperationGroup;
import com.neusoft.neuiotms.dcom.model.QOperationGroup;
import com.neusoft.neuiotms.dcom.repository.CustomizedOperationGroupRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedOperationGroupRepositoryImpl extends AbstractRepository
        implements CustomizedOperationGroupRepository {

    @Override
    public List<OperationGroup> index(final Searchable searchable) {
        final QOperationGroup qOperationGroup = QOperationGroup.operationGroup;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<OperationGroup> query = this.query().selectFrom(qOperationGroup);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<OperationGroup> search(final Pageable pageable, final Searchable searchable) {
        final QOperationGroup qOperationGroup = QOperationGroup.operationGroup;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<OperationGroup> query = this.query().selectFrom(qOperationGroup);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return OperationGroup.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QOperationGroup qOperationGroup = QOperationGroup.operationGroup;

        // 组名称检索
        where.and(super.containsStr(qOperationGroup.name, searchable, "name"));

        // 单位
        final Long institutionsId = searchable.getLongValue("institutionsId");
        if (null != institutionsId) {
            where.and(super.equalsLong(qOperationGroup.institutions.any().id, searchable, "institutionsId"));
        }

        // 单位编码
        final String institutionsCode = searchable.getStrValue("institutionsCode");
        if (!StringUtils.isEmpty(institutionsCode)) {
            where.and(super.equalsStr(qOperationGroup.institutions.any().code, searchable, "institutionsCode"));
        }

        //        if (!StringUtils.isEmpty(searchable.getStrValue("username"))) {
        //            where.and(super.equalsStr(qOperationGroup.institutions.any().residentUserCode, searchable, "username"));
        //        }
        //        if (!StringUtils.isEmpty(searchable.getStrValue("username"))) {
        //            where.or(super.equalsStr(qOperationGroup.institutions.any().residentUserCode, searchable, "username"));
        //            where.or(
        //                    super.equalsStr(qOperationGroup.institutions.any().users.any().username, searchable, "username"));
        //        }
        return where;
    }
}
